<?php

  class FenxiaoAction extends CommonAction{
    
	public function index(){
	
	  if(isset($_GET['keywords'])){
	  
	    $this->keyword=$_GET['keywords'];
		
		$keyword=$_GET['keywords'];
		
		$where['username']=array("LIKE","%$keyword%");
	  
	  
	  }
	
	  $where['type']=array('eq',0);
      
	  $re=$this->page('dailiuser','10','id desc',$where);
	  
	  $i=-1;
	  
	  foreach($re as $v){
	  
	    $i++;
	  
	    $num=M('dailiuser')->where('upid="%s"',$v['id'])->field('id')->count();
		
		$re[$i]['num']=$num;
		
		
		$xiaji=M('dailiuser')->where('fenxiaoid="%s"',$v['id'])->field('id')->select();
		
		$ids=array();
		
		foreach($xiaji as $vv){
		
		  $ids[]=$vv['id'];
		
		}
		
		$w['status']=array('eq','1');
		$w['tixianid']=array('IN',$ids);
		
		$total=M('tixian')->where($w)->field('money')->select();
		
		//dump(M('tixian')->getlastsql());
		
		$xiajimoney=0;
		
		foreach($total as $vvv){
		
		  $xiajimoney+=$vvv['money'];
		
		}
		
		$re[$i]['xiajimoney']=$xiajimoney;
		
		
		
	  
	  }
	  
	  
      	
	  $this->re=$re;	
		
	  $this->display();
	
	}
	
	
	
	public function xiangxi(){
	
	 
	
	
	  $where['upid']=array('eq',$_GET['id']);
	
	  $re=$this->page('dailiuser','10','id desc',$where);
	
	 /* $i=-1;
	  
	  foreach($re as $v){
	  
	    $i++;
		
		
	  
	  
	  }
	*/
	
	
	  $this->re=$re;
	
	
	  $this->display();
	
	}
	
	
	public function shijianduan(){
	
	   if(isset($_GET['start'])){
	  
	     $this->start=$_GET['start'];
		 $this->end=$_GET['end'];
		 
		 $start=strtotime($_GET['start']);
		 $end=strtotime($_GET['end']);
	  
	     if($start>$end){
		 
		   $this->error('开始时间不能大于结束时间');
		 
		 }
	  
	     $end=$end+86400;
		 
		 $where['time']=array(array('gt',$start),array('lt',$end),'and');
	  
	  }
	  
	   $ids=array();
	   
	   $xiaji=M('dailiuser')->where('fenxiaoid="%s"',$_GET['id'])->select();
	   
	   foreach($xiaji as $v){
	   
	     $ids[]=$v['id'];
	   
	   }
	   
	   
	   $where['status']=array('eq','1');
	   $where['tixianid']=array('IN',$ids);
	  
	  
	  $re= $this->page('tixian','10','id desc',$where);
	  
	   
	    if(isset($_GET['start'])){
		
		  $sm=M('tixian')->where($where)->select();
		  
	
		  $money=0;
		  
		  foreach($sm as $v){
		  
		    $money+=$v['money'];
		  
		  }
		  
		  $this->money=$money;
		}
	  
	  
	 // dump($re);
	  
	  //dump(M('tixian')->getlastsql());
	  
	  $i=-1;
	  
	  foreach($re as $v){
	  
	    $i++;
		
		$user=M('dailiuser')->where('id="%s"',$v['tixianid'])->field('username')->find();
		
		$re[$i]['username']=$user['username'];
		
	  
	  }
	  
	  
	   $this->re=$re;
	   
	   
	  
	  
	   $this->display();
	
	}
	
	
	
	
	public function add(){
	
	  if(!empty($_POST)){
	  
	    if(I('password')!=I('repass')){
		
		  $this->error('两次密码输入不一致');
		
		}
		
		
		$result=M('dailiuser')->where('username="%s"',I('username'))->find();
		
		if($result!=null){
		
		  $this->error('用户名已经存在');
		
		}
		
		
		$arr['username']=I('username');
		$arr['password']=md5(I('password'));
		$arr['regtime']=time();
		$str='1234567890abcdefghigklmnopqrstuvwxyz';
		$str=str_shuffle($str);
		$str=substr($str,0,6);
		$arr['keyword']=$str.time();
		
		$re=M('dailiuser')->add($arr);
		
		if($re){
		
		  $this->success('添加成功');exit;
		
		}else{
		
		  $this->error('添加失败');
		
		}
		
		
	  
	  }
	
	  $this->display();
	
	}
	
	
	
	 public function del(){
	  
	  
	
	  $this->ajaxReturn((M('dailiuser')->where('id="%s"',$_POST['id'])->delete())?1:2);
	
	}
	
  
  
  
  }